package com.cal.utms.action.dealing;

import java.util.ArrayList;
import java.util.List;

import com.cal.utms.dao.dealing.DealDao;
import com.cal.utms.dao.maintenance.NewFundDao;
import com.cal.utms.dto.dealing.RollOverDto;
import com.cal.utms.entity.Deal;
import com.cal.utms.entity.DealInstrumentMap;
import com.cal.utms.entity.DealType;
import com.cal.utms.entity.Fund;
import com.cal.utms.entity.Instrument;
import com.cal.utms.entity.InstrumentType;
import com.cal.utms.entity.Issuer;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class RollOverAction extends ActionSupport implements ModelDriven<RollOverDto> {


	private static final long serialVersionUID = 1L;
	private RollOverDto rollOverDto = new RollOverDto();

	@Override
	public RollOverDto getModel() {
		return rollOverDto;
	}
	
	public String getFundInfoById(){
		NewFundDao fundDao = new NewFundDao();
		int fundId = 0;
		if(rollOverDto.getQueryOne()!=null){
			try{
				fundId = Integer.parseInt(rollOverDto.getQueryOne());
			}catch(NumberFormatException nfe){
				rollOverDto.setStatus("Invalid Fund ID");
				rollOverDto.setSuccess(false);
				return Action.SUCCESS;
			}
			Fund fund = fundDao.getById(fundId);
			if(fund!=null){
				rollOverDto.setFund(fund);
			}else{
				rollOverDto.setStatus("Invalid Fund ID2");
				rollOverDto.setSuccess(false);
			}
		}else{
			rollOverDto.setStatus("Empty Fund ID");
			rollOverDto.setSuccess(false);
		}		
		return Action.SUCCESS;
	}
	
	public String getFundInfoByCode(){
		NewFundDao fundDao = new NewFundDao();

		if(rollOverDto.getQueryOne()!=null){			
			Fund fund = fundDao.getByCode(rollOverDto.getQueryOne());
			if(fund!=null){
				fund.setFundManager(null);
				fund.setCustodian(null);
				fund.setCustodianContactPersons(null);
				rollOverDto.setFund(fund);
			}else{
				rollOverDto.setStatus("Invalid Fund ID2");
				rollOverDto.setSuccess(false);
			}
		}else{
			rollOverDto.setStatus("Empty Fund ID");
			rollOverDto.setSuccess(false);
		}		
		return Action.SUCCESS;
	}
	
	public String getConfDealsByDealType(){
		DealDao dealDao = new DealDao();
		List<Deal> resList = new ArrayList<Deal>();
		String dealTypeCode = rollOverDto.getDealTypeCode();
		List<Deal> dealList = null;
		if(dealTypeCode==null){
			rollOverDto.setStatus("No DealTypeCode Found!");
			rollOverDto.setSuccess(false);
			return Action.SUCCESS;
		}
		dealList = dealDao.getRollOverListByDealType(dealTypeCode);
		if(dealList == null){
			rollOverDto.setStatus("System Error!");
			rollOverDto.setSuccess(false);
			return Action.SUCCESS;
		}else{			
			Deal resDeal=null;
			for(Deal deal: dealList){
				resDeal = new Deal();
				resDeal.setId(deal.getId());
				resDeal.setDealCode(deal.getDealCode());
				Fund resFund = new Fund();
				resFund.setFundCode(deal.getFund().getFundCode());
				resDeal.setFund(resFund);
				Issuer resIssuer = new Issuer();
				resIssuer.setCode(deal.getIssuer().getCode());
				resDeal.setIssuer(resIssuer);
				resDeal.setMaturityDate(deal.getMaturityDate());
				resDeal.setMaturityValue(deal.getMaturityValue());
				DealType resDealType = new DealType();
				resDealType.setCode(deal.getDealType().getCode());
				resDeal.setDealType(resDealType);
				if(resDealType.getCode().equals("PURCH")){
					InstrumentType insType = new InstrumentType();
					if(deal.getDealInstrumentMaps().isEmpty()){
						System.out.println("Purchased Instrument Cannot found for Purchase Deal: "+deal.getDealCode());
					}else{
						insType.setInstrumentName(deal.getDealInstrumentMaps().get(0).getInstrument().getInstrumentType().getInstrumentName());
					}					
					Instrument ins = new Instrument();
					ins.setInstrumentType(insType);
					DealInstrumentMap dim = new DealInstrumentMap();
					dim.setInstrument(ins);
					List<DealInstrumentMap> dimList = new ArrayList<DealInstrumentMap>();
					dimList.add(dim);
					resDeal.setDealInstrumentMaps(dimList);
				}				
				resList.add(resDeal);
			}
		}
		rollOverDto.setDealList(resList);		
		return Action.SUCCESS;
	}
	

}
